////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// The MIT License (MIT)                                                      //
//                                                                            //
// Copyright (c) 2013 Charles Teague                                          //
//                                                                            //
// Permission is hereby granted, free of charge, to any person obtaining a    //
// copy of this software and associated documentation files (the "Software"), //
// to deal in the Software without restriction, including without limitation  //
// the rights to use, copy, modify, merge, publish, distribute, sublicense,   //
// and/or sell copies of the Software, and to permit persons to whom the      //
// Software is furnished to do so, subject to the following conditions:       //
//                                                                            //
// The above copyright notice and this permission notice shall be included in //
// all copies or substantial portions of the Software.                        //
//                                                                            //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    //
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    //
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        //
// DEALINGS IN THE SOFTWARE.                                                  //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using RPSDAL;
using RPSDAL.CollectionClasses;
using RPSDAL.EntityClasses;
using RPSDAL.HelperClasses;

using SD.LLBLGen.Pro.ORMSupportClasses;

public partial class UserCallsList : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        grdUsers.PageSize = Parameters.PageSize;
        LoadUserCalls();
    }

    protected void LoadUserCalls()
    {
        UserCollection cUsers = new UserCollection();
        cUsers.GetMulti(null);

        IPredicateExpression filterUsers = new PredicateExpression();
        int busyUsers = 0;
        for (int i = 0; i < cUsers.Count; i++)
        {
            CallCollection cCalls = new CallCollection();
            IPredicateExpression filter = new PredicateExpression();
            filter.Add(CallFields.UserId == cUsers[i].UserId);
            filter.AddWithAnd(CallFields.Done == false);
            int noCallsUser = cCalls.GetDbCount(filter);

            if (noCallsUser >= cUsers[i].MaxCallPerWeek)
            {
                filterUsers.AddWithAnd(UserFields.UserId != cUsers[i].UserId);
                busyUsers++;
            }
        }

        if (busyUsers == cUsers.Count)
        {
            grdUsers.DataSource = cUsers;
            grdUsers.DataBind();
        }
        else
        {
            cUsers.GetMulti(filterUsers);
            grdUsers.DataSource = cUsers;
            grdUsers.DataBind();
        }

        for (int i = grdUsers.Rows.Count - 1; i >= 0; i--)
        {
            Label lblNoCalls = (Label)grdUsers.Rows[i].Cells[3].FindControl("lblNoCalls");

            CallCollection cCalls = new CallCollection();
            IPredicateExpression filter = new PredicateExpression();
            filter.Add(CallFields.UserId == cUsers[i].UserId);
            filter.AddWithAnd(CallFields.Done == false);
            int noCallsUser = cCalls.GetDbCount(filter);
            lblNoCalls.Text = noCallsUser.ToString();
        }

        CallCollection cAllCalls = new CallCollection();
        IPredicateExpression filterDone = new PredicateExpression();
        filterDone.Add(CallFields.Done == false);
        int noCalls = cAllCalls.GetDbCount(filterDone);
        if (noCalls <= 0)
        {
            btnAssignCalls.Enabled = false;
        }

        lblCallsToAssign.Text = "No. of calls to be assigned: " + noCalls.ToString();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    
    }
    
    protected void AssignCalls(object sender, EventArgs e)
    {
        
        for (int i = 0; i < grdUsers.Rows.Count; i++)
        {
            if (((Literal)grdUsers.Rows[i].Cells[4].FindControl("literalId")).Text.Contains("checked")) 
            {
                Response.Redirect("AssignCalls.aspx?id=" + Global.EncryptString(((TextBox)grdUsers.Rows[i].Cells[4].FindControl("txtId")).Text));
            }
        }       
    }

    

    protected void grdUsers_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {           
            Literal output = (Literal)e.Row.FindControl("literalId");
            output.Text = @"<input type=""radio"" name=""rdAssign"" id=""RowSelector{0}"" value="""+e.Row.RowIndex.ToString()+@"""";

            if (SelectedIndex == e.Row.RowIndex)
            { 
                output.Text += " checked";
            }

            output.Text +=  @"/>";    
        }
    }

    private  int SelectedIndex
    {
        get
        {
            if (String.IsNullOrEmpty(Request.Form["rdAssign"]))
            {
                return 0;
            }
            else
            {
               return Convert.ToInt32(Request.Form["rdAssign"]);
            }
        }

    }

    protected void grdUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdUsers.PageIndex = e.NewPageIndex;
        LoadUserCalls();
    }


}
